//1.引入vue
// import Vue from 'vue'
//2.引入axios库
import Axios from 'axios';
import { GetRouter } from '@/stores/modules/RouterList'
import { config } from '@/utils/config'//引入公用文件
// axiosNew.defaults.timeout = 3000;//请求超时时间
const store = {
    token: null
}
const service = Axios.create({
    withCredentials: true,
    baseURL: `${config?.host}`,
    timeout: 5000,
    headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "content-type", },
})

service.interceptors.request.use((config: any) => {
    // const store = GetRouter()
    const store = JSON.parse(localStorage.getItem('token') as any)
    if (store.token) {
        // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改,从内存中取得
        config.headers['Authorization'] = store.token
    }
    // config.headers = {
    //   'Content-Type': 'application/json' // 设置很关键
    // }
    return config
},
    error => {
        console.log(error)
        Promise.reject(error)
    }
)

service.interceptors.response.use(response => {
    // const store = GetRouter()
    const authorization = response["headers"].authorization;
    if (authorization) {
        // store.getToken(authorization)
    }
    if (response.data.code === 402 && response.data.subCode === 804 || response.data.code === 402 && response.data.subCode === 402) {
        // store.deleteToken("")
        // window.location.href = 
    }
    return response;
},
    error => {
        return Promise.reject(error)
    }
)

export default service
